package cn.itcast.service.impl;

import cn.itcast.dao.CheckItemDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.CheckItem;
import cn.itcast.service.CheckItemService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 新增检查项
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    /**
     * 查询检查项
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        List<CheckItem> rows = checkItemDao.findPage(queryPageBean);
        PageInfo<CheckItem> pageInfo = new PageInfo<CheckItem>(rows);
        long total = pageInfo.getTotal();
        return new PageResult(total,rows);
    }

    /**
     * 修改检查项
     * @param checkItem
     */
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    /**
     * 根据id删除检查项
     * @param id
     */
    @Override
    public void delete(int id) {
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count>0){
            //当前检察项已经被关联检查组,不允许删除
            new RuntimeException();
        }
            checkItemDao.delete(id);


    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
